/**
 *
 * Copyright 2009 Marco Speranza <marco.speranza79@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.commons.database.jconnectionpool;

import java.sql.SQLException;

import org.apache.log4j.Logger;

/**
 * 
 */
public class IdlePooledConnectionTh implements Runnable {
    /* standard header, for source files, for automatic identification by ident */
    private static final String rcsid = "$Header: IdlePooledConnectionTh.java,v 1.1 2007/01/19 10:54:55 Administrator Exp $";

    /*-----------------------------------------------------------------------*/
    private static Logger log = Logger.getLogger(IdlePooledConnectionTh.class);

    public static long DEFAULT_MISSING_TIME = 2000L;
    public static long DEFAULT_PACE_TIME = 1000L;

    private PoolConnectionManager manager;

    private boolean shutdown = true;
    private Thread t = null;

    private long missingTime;
    private long paceTime;

    public IdlePooledConnectionTh(PoolConnectionManager manager) {
	this(manager, DEFAULT_MISSING_TIME, DEFAULT_PACE_TIME);
    }

    public IdlePooledConnectionTh(PoolConnectionManager manager,
	    long missingTime, long paceTime) {
	this.manager = manager;
	this.missingTime = missingTime;
	this.paceTime = paceTime;
	start();
    }

    public void start() {
	log.info("Start " + IdlePooledConnectionTh.class.getName());
	shutdown = false;
	if (t != null) {
	    t.interrupt();
	    t = null;
	}
	t = new Thread(this, "IdlePooledConnectionTh");
	t.start();

    }

    public void stop() {
	synchronized (t) {
	    shutdown = true;
	    t.notifyAll();
	}

	log.debug("Stop " + IdlePooledConnectionTh.class.getName());
    }

    public void run() {
	try {
	    while (!shutdown) {
		WrapPooledConnection item = null;

		if (manager.idleConnection.size() > 0) {
		    item = (WrapPooledConnection) manager.idleConnection.get(0);
		}

		if ((item != null)
			&& ((item.getAvaiableTime() + missingTime) < System
				.currentTimeMillis())) {
		    log.debug("Close physical ideConnection Th item: " + item);

		    try {
			item.getPooledConnection()
				.removeConnectionEventListener(item);
			item.getPooledConnection().close();
		    } catch (SQLException e) {
			log.error(e, e);
		    }
		    manager.idleConnection.remove(0);
		} else {
		    synchronized (t) {
			t.wait(paceTime);
		    }

		    // Thread.sleep(paceTime);
		}
	    }
	    log.info("Shutdown arrived!! IdlePooledConnectionTh finished.");
	} catch (InterruptedException e) {
	    log.info("Interrupted event. IdlePooledConnectionTh finished.");
	}
    }

}
